<template>
  <div class="withdrawal divBox">
    <el-card class="box-card" shadow="hover" :body-style="{ padding: '0px' }">
      <template class="clearfix" v-slot:header>
        <div class="container">
          <el-form size="small" label-width="100px">
            <el-form-item label="提现状态：">
              <el-radio-group v-model="tableFrom.status" size="small" @change="getApplyList('1')">
                <el-radio-button :label="-99">全部</el-radio-button>
                <el-radio-button :label="0">审核中</el-radio-button>
                <el-radio-button :label="1">已提现</el-radio-button>
                <el-radio-button :label="2">已拒绝</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="提现方式：">
              <el-radio-group v-model="tableFrom.type" size="small" @change="getApplyList('1')">
                <el-radio-button :label="-99">全部</el-radio-button>
                <el-radio-button :label="0">银行卡</el-radio-button>
                <el-radio-button :label="1">微信</el-radio-button>
                <el-radio-button :label="2">支付宝</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-row>
              <el-col v-bind="grid">
                <el-form-item label="搜索：" class="search-select">
                  <el-input v-model="tableFrom.keyword" placeholder="微信号/姓名/支付宝账号/银行卡号" size="small" clearable>
                    <el-button slot="append" icon="el-icon-search" size="small" @click="getApplyList('1')"></el-button>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </template>
    </el-card>

    <div style="margin-top: 15px;">
      <cart-list :cartList="cartList"></cart-list>
    </div>

    <div style="margin-bottom: 15px;">
      <el-alert :closable="false" center show-icon title="商户号若支持企业付款则会直接打款且打款需要商户号余额充足，否则需要手动打款"></el-alert>
    </div>

    <el-card class="box-card" shadow="hover">
      <el-table
        v-loading="listLoading"
        :data="tableData"
        style="width: 100%"
        size="small"
        class="table"
        highlight-current-row
      >
        <el-table-column prop="id" label="ID" width="60"></el-table-column>
        <el-table-column label="用户信息" min-width="180">
          <template slot-scope="{ row }">
            <div>用户昵称：{{ row.user.realName || row.user.nickName }}</div>
            <div>用户id：{{ row.userId }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="提现金额" min-width="120">
          <template slot-scope="{ row }">
            <p>{{ row.price | NumFormat }}</p>
          </template>
        </el-table-column>
        <el-table-column label="提现方式" min-width="120">
          <template slot-scope="{ row }">
            <!-- 银行卡【start】 -->
            <div class="type" v-if="row.type === 0">
              <div class="item">姓名:{{ row.account }}</div>
              <div class="item">银行卡号:{{ row.bankCode }}</div>
              <div class="item">银行开户地址:{{ row.bankName }}</div>
            </div>
            <!-- 银行卡【end】 -->
            <!-- 微信【start】 -->
            <div class="type" v-if="row.type === 1">
              <div class="item">昵称:{{ row.user.realName || row.user.nickName }}</div>
              <div class="item">微信号:{{ row.account }}</div>
            </div>
            <!-- 微信【end】 -->
            <!-- 支付宝【start】 -->
            <div class="type" v-if="row.type === 2">
              <div class="item">姓名:{{ row.user.realName || row.user.nickName }}</div>
              <div class="item">支付宝号:{{ row.account }}</div>
            </div>
            <!-- 支付宝【end】 -->
          </template>
        </el-table-column>
        <el-table-column label="收款码" min-width="80">
          <template slot-scope="{ row }">
            <div class="taoBox_img" v-viewer v-if="row.type === 1 || row.type === 2">
              <img v-lazy="$uploadURL(row.qrcode)" :alt="$uploadURL(row.qrcode)">
            </div>
          </template>
        </el-table-column>
        <el-table-column label="添加时间" min-width="150">
          <template slot-scope="{ row }">
            <span>{{ row.createDateUTC | uctTimeFileter }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          min-width="200"
        >
          <template slot-scope="{ row }">
            <span class="remark">{{ row.remark }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" min-width="200">
          <template slot-scope="{ row }">
            <div class="status" v-if="row.status === 0">
              <div class="statusVal">申请中</div>
              <div>
                <el-button @click="invalid(row.id)" type="danger" icon="el-icon-close" size="mini" class="btnItem">驳回</el-button>
                <el-button @click="adopt(row.id)" type="primary" icon="el-icon-check" size="mini" class="btnItem">通过</el-button>
              </div>
            </div>
            <div class="statusVal" v-if="row.status === 1">提现通过</div>
            <div class="statusVal" v-if="row.status === 2">提现未通过<br/>未通过原因：{{ row.failMsg }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="80" fixed="right" align="center">
          <template slot-scope="{ row }">
            <el-button type="text" size="small" @click="handleEdit(row.id, row.type)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="applyBox">
        <pagination
          v-show="pagination.total > 0"
          :total="pagination.total"
          :page.sync="pagination.page"
          :limit.sync="pagination.limit"
          :page-sizes="[10, 20, 30, 50]"
          @pagination="applyChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
  import cartList from "@/components/card";
  import {
    getApplyListApi,
    getApplyBalanceApi,
    postApplyStatusApi
  } from '@/api/financial';

  export default {
    name: "index",
    components: {
      cartList
    },
    data() {
      return {
        grid: {
          xl: 5,
          lg: 7,
          md: 12,
          sm: 12,
          xs: 24
        },
        tableFrom: {
          status: -99,
          type: -99,
          keyword: ''
        },
        cartList: [],
        tableData: [],
        listLoading: false,
        pagination: {
          page: 1,
          limit: 10,
          total: 0
        }
      };
    },
    mounted() {
      this.getApplyList();
      this.getBalance();
    },
    methods: {
      // 编辑
      handleEdit(id, type) {
        const _this = this;
        this.$modal({
          componentProps: {
            id: id
          },
          component: (resolve) => {
            if (type === 0) require(['./components/editApplyBank'], resolve);
            else if (type === 1 || type === 2) require(['./components/editApplyWeChat'], resolve);
          },
          props: {
            title: '编辑',
            customClass: 'zdyDialogCls editApplyDialogCls',
            width: '100%'
          },
          methods: {
            getApplyCreat() {
              _this.$set(_this.pagination, 'page', 1);
              _this.getApplyList();
            }
          }
        });
      },
      // 驳回
      invalid(id) {
        const _this = this;
        this.$modal({
          componentProps: {
            id: id
          },
          component: (resolve) => require(['./components/editAudit'], resolve),
          props: {
            title: '未通过原因',
            customClass: 'zdyDialogCls AuditDialogCls',
            width: '100%'
          },
          methods: {
            getAuditList() {
              _this.$set(_this.pagination, 'page', 1);
              _this.getApplyList();
            }
          }
        });
      },
      // 通过
      adopt(id) {
        const _this = this;
        this.$confirm('审核通过', {
          title: '审核通过',
          message: this.$createElement('div', null, ['确定要审核通过吗？', '审核通过后将无法恢复，请谨慎操作！'].map((item) => this.$createElement('p', null, item))),
          showCancelButton: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          postApplyStatusApi(id, {
            status: 1
          }).then(() => {
            _this.$message.success('审核成功');
            _this.getApplyList();
          }).catch(err => {
            _this.$message.warning(err || "审核失败");
          });
        }).catch(() => {
          _this.$message({
            type: 'info',
            message: '已取消审核'
          });
        });
      },
      // 获取提现统计信息
      getBalance() {
        getApplyBalanceApi().then(res => {
          this.$set(this, 'cartList', [
            { name: '待提现金额', count: res.withdrawal_Price_Wait, color: '#4d7cfe', class: 'one', icon: 'icon-qianbao1' },
            { name: '佣金总金额', count: res.commission_Price, color: '#ffab2b', class: 'two', icon: 'icon-a-zijinqiancaixianjin-07' },
            { name: '已提现金额', count: res.withdrawal_Price_Ok, color: '#6dd230', class: 'three', icon: 'icon-qianbao' },
            { name: '未提现金额', count: res.withdrawal_Price_No, color: '#ff85c0', class: 'four', icon: 'icon-qiandai' }
          ]);
        })
      },
      // 获取提现记录
      getApplyList(num) {
        this.$set(this, 'listLoading', true);
        let list = {
          pageNumber: num ? num : this.pagination.page,
          pageSize: this.pagination.limit,
          orderBy: 'id desc',
          type: this.tableFrom.type === -99 ? -1 : this.tableFrom.type,
          statuses: this.tableFrom.status,
          keyword: this.tableFrom.keyword
        };
        if (list.statuses === -99) delete list.statuses;
        getApplyListApi(list).then(res => {
          this.$set(this, 'tableData', res.data);
          this.$set(this.pagination, 'total', res.head.totalCount);
          this.$set(this, 'listLoading', false);
        }).catch(() => {
          this.$set(this.pagination, 'total', 0);
          this.$set(this, 'tableData', []);
          this.$set(this, 'listLoading', false);
        });
      },
      applyChange(apply) {
        this.$set(this.pagination, 'page', apply.page);
        this.$set(this.pagination, 'limit', apply.limit);
        this.getApplyList();
      }
    }
  }
</script>

<style lang="scss" scoped>
  .withdrawal {
    ::v-deep .cart_box {
      cursor: pointer;
      margin-bottom: 15px;
    }
    .box-card {
      ::v-deep.el-card__header {
        border: 0 solid transparent !important;
      }
    }
    .taoBox_img {
      width: 36px;
      height: 36px;
      border-radius: 4px;
      overflow: hidden;
      cursor: pointer;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .btnItem {
      padding: 4px 8px !important;
    }
  }
</style>

<style lang="scss">
  .el-dialog.AuditDialogCls {
    max-width: 520px;
  }
  .el-dialog.editApplyDialogCls {
    max-width: 700px;
  }
</style>
